﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM.Users
{
    public class PermissionGroup
    {
        private string m_name;
        private List<string> m_rights;

        public string Name
        {
            get
            {
                return m_name;
            }
        }

        /// <summary>
        /// Get if the permission is enabled
        /// </summary>
        /// <param name="right">The permission name</param>
        /// <returns>True if enabled</returns>
        public bool this[string right]
        {
            get
            {
                return this.m_rights.Contains(right);
            }
        }

        public string[] Permissions
        {
            get
            {
                return m_rights.ToArray();
            }
        }

        public PermissionGroup()
        {
            m_name = "";
            m_rights = new List<string>();
        }

        public PermissionGroup(string name)
        {
            m_name = name;
            m_rights = new List<string>();

            Data.DatabaseWriter.Instance.Queue(PermissionGroupManager.Instance.getFile());
        }

        public void addRight(string name, bool from_multi = false)
        {
            lock (m_rights)
            {
                if (PermissionList.isValidPermission(name) && !m_rights.Contains(name))
                    m_rights.Add(name);
            }

            if (!from_multi)
                Data.DatabaseWriter.Instance.Queue(PermissionGroupManager.Instance.getFile());
        }

        public void addRights(string[] rights)
        {
            for (int i = 0, j = rights.Length; i < j; i++)
                addRight(rights[i],true);

            Data.DatabaseWriter.Instance.Queue(PermissionGroupManager.Instance.getFile());
        }

        public void removeRight(string name)
        {
            lock (m_rights)
            {
                if (m_rights.Contains(name))
                    m_rights.Remove(name);
            }

            Data.DatabaseWriter.Instance.Queue(PermissionGroupManager.Instance.getFile());
        }

        public void clearRights()
        {
            lock (m_rights)
            {
                m_rights.Clear();
            }

            Data.DatabaseWriter.Instance.Queue(PermissionGroupManager.Instance.getFile());
        }

        public string serialize()
        {
            StringBuilder final = new StringBuilder();

            foreach (string friend in this.m_rights)
                final.Append(friend + ";");

            if (this.m_rights.Count > 0)
                return final.ToString().Substring(0, final.ToString().Length - 1);
            else
                return "";
        }

        public void unserialize(string serialized)
        {
            this.m_rights.Clear();

            if (serialized.Length > 0)
            {
                string[] friends = serialized.Split(new char[] { ';' });
                this.m_rights.AddRange(friends);
            }
        }
    }
}
